package com.pipodi.itasamobile.parsers;

import java.util.ArrayList;
import java.util.List;



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


import com.pipodi.itasamobile.NewsViewerActivity;
import com.pipodi.itasamobile.connections.Connection;
import com.pipodi.itasamobile.connections.ConnectionForImg;
import com.pipodi.itasamobile.model.News;
import com.pipodi.itasamobile.model.interfaces.NewsInterface;
import com.pipodi.itasamobile.utils.NewsListAdapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;

public class NewsParser extends AsyncTask<String, String, String>{
	
	private List<NewsInterface> items;
	private ListView listView;
	private Context context;
	private LayoutInflater inflater;
	private ProgressBar progress;
	
	public NewsParser(ListView view, Context context, LayoutInflater inflater, ProgressBar progress){
		super();
		this.listView = view;
		this.items = new ArrayList<NewsInterface>();
		this.context = context;
		this.inflater = inflater;
		this.progress = progress;
	}
	
	public NewsParser(List<NewsInterface> news, ListView view, Context context, LayoutInflater inflater){
		super();
		this.items = news;
		this.listView = view;
		this.context = context;
		this.inflater = inflater;
	}
	
	@Override
	protected String doInBackground(String... params) {
		Document doc = Connection.connectToAPIUrl("https://api.italiansubs.net/api/rest/news?&apikey=ce0a811540244ad5267785731a5c37a0");
		NodeList nList = doc.getElementsByTagName("news");
		for (int temp = 1; temp < 11 ; temp++) {
			Node nNode = nList.item(temp);
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				Element eElement = (Element) nNode;
				String title = eElement.getElementsByTagName("show_name").item(0).getTextContent();
				String episode = eElement.getElementsByTagName("episode").item(0).getTextContent();
				String release = eElement.getElementsByTagName("submit_date").item(0).getTextContent();
				String photo_url = eElement.getElementsByTagName("image").item(0).getTextContent();
				int id = Integer.parseInt(eElement.getElementsByTagName("id").item(0).getTextContent());
				release = DateFormatParser.changeDateFormat(release);
				Bitmap photo = ConnectionForImg.getPhotoFromURL(photo_url);
				NewsInterface news = new News(id, title, episode, release, photo);
				this.items.add(news);
				Log.e("NEWS", news.toString());
			}
		}
		return null;
	}
	
	@Override
	protected void onPostExecute(String result){
		NewsListAdapter adapter = new NewsListAdapter(context,
				inflater, this.items);
		Log.e("ADAPTER", "Adapter inizializzato");
		listView.setAdapter(adapter);
		this.progress.setVisibility(View.INVISIBLE);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				NewsInterface temp = (NewsInterface) listView
						.getItemAtPosition(position);
				String current_title = temp.getTitle();
				String current_episode = temp.getEpisode();
				String current_release = temp.getReleaseDate();
				Bitmap current_photo = temp.getPhoto();
				Intent openingIntent = new Intent(context,
						NewsViewerActivity.class);
				openingIntent.putExtra("TITLE", current_title);
				openingIntent.putExtra("EPISODE", current_episode);
				openingIntent.putExtra("RELEASE", current_release);
				openingIntent.putExtra("PHOTO", current_photo);
				context.startActivity(openingIntent);
			}
		});
	}
}
